define(['moment'], function (moment) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', '$timeout', 'config',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, $timeout, config ) {
            // 流程
            $scope.phases = [
                'process_feeder',
                'process_material'
            ];

            let element_barcode_no;//pda掃描
            $scope.init = function(){
                // 使用者和環境資料
                $scope.user_name = config.cache.name;
                $scope.envInfo = $filter('translate')('Login.env.'+config.setting.environment);
                // 預設流程狀態
                $scope.phases_index = 0;
                $scope.disable_control = config.cache.sys_parameter["SMTInfo.SMTPartDisableControl"] //禁用物料卡控
                $scope.barcode = '';
                
                // 自動隱蔽鍵盤
                element_barcode_no = document.getElementById('input_barcode');
                makeBarcodeInput(element_barcode_no);
                element_barcode_no.focus();
            };

            $scope.back = function(){
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('menu', true);
            };

            // QRCode輸入
            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        $scope.barcode = result.text;
                        input_barcode_no_callback(result.text);
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    },
                    orientation : 'portrait'
                });
            };

            // barcode輸入
            $scope.input_barcode = function(barcode){
                input_barcode_no_callback(barcode);
            };

            // 流程狀態
            $scope.PhaseStatus = function(index){
                if($scope.phases_index > index)
                    return ['prev'];
                else if($scope.phases_index == index)
                    return ['now'];
                else
                    return ['next'];
            }

            // 回上一步流程
            $scope.BackToPhase = function (index){
                if($scope.phases_index > index){
                    $scope.phases_index = index;
                    if(index == 0){
                        $scope.feeder_type_no = '';
                        $scope.feeder_no = '';
                        $scope.feeder_state = '';
                        $scope.last_maintain_date = '';

                        $scope.reel_no = '';
                        $scope.material_no = '';
                        $scope.material_product_no = '';
                        $scope.material_item_spec = '';
                        $scope.mfg_date = '';
                        $scope.exp_date = '';
                    }
                }
                element_barcode_no.focus();
            };

            function reel_process(itemList){
                // 在庫 0
                if(itemList[0].is_by_lot=='N'){
                    if(itemList[0].state == '0'){
                        execute_state_zero(itemList);
                    // 80 報廢
                    } else if(itemList[0].state == '80'){
                        $scope.$root.showAlert($filter('translate')('SMT.common.msg.reel_scrapped'));
                    // 40 已上料
                    } else if (itemList[0].state == '40'){
                        $scope.$root.showAlert($filter('translate')('SMT.common.msg.reel_already_feeding'));
                    // 60 已上線
                    } else if (itemList[0].state == '60'){
                        $scope.$root.showAlert($filter('translate')('SMT.common.msg.reel_already_online'));
                    // 不存在
                    } else {
                        $scope.$root.showAlert($filter('translate')('SMT.common.msg.no_reel'));
                    };
                }else{
                    // 80 報廢
                    if(itemList[0].state == '80'){
                        $scope.$root.showAlert($filter('translate')('SMT.common.msg.reel_scrapped'));
                    }else{
                        execute_state_zero(itemList);
                    }
                }
            }

            function execute_state_zero(itemList){//add by Oliver for M#74017 鑫森海SMT修改 state 0 時要執行的動作，統一寫在這裡再放入判斷式
                $scope.phases_index = 1;
                $scope.reel_no = itemList[0].reel_no;
                $scope.material_no = itemList[0].material_no;
                $scope.material_product_no = itemList[0].product_no;
                $scope.material_item_spec = itemList[0].item_spec;
                $scope.mfg_date = itemList[0].mfg_date;
                $scope.exp_date = itemList[0].exp_date;
                $scope.remain_time = itemList[0].OpenTime;                                             
                var content = {
                    feeder_type_no: $scope.feeder_type_no,
                    feeder_no: $scope.feeder_no,
                    material_no: $scope.material_no,
                    reel_no: $scope.reel_no
                };
                feeder_reel_set_process(content,function(){
                    $scope.BackToPhase(0);
                    $scope.$root.showToast($filter('translate')('common.msg.success'));
                });
            }

            // barcode輸入 callback
            function input_barcode_no_callback(barcode){
                if(barcode != ''){
                    // feeder流程
                    if($scope.phases_index == 0){
                        feeder_info_get(barcode, function(itemList){
                            // 在庫狀態才能成功，其他狀態提示異常
                            if(itemList && itemList.length > 0){
                                // 在庫 0
                                if(itemList[0].state == '0'){
                                    var content = {
                                        feeder_no : itemList[0].feeder_no
                                    };
                                    feeder_check_process(content,function(data, response){
                                        if(response.Message != ''){
                                            $scope.$root.showAlert(response.Message);
                                        } else {
                                            $scope.phases_index = 1;
                                            $scope.feeder_type_no = itemList[0].feeder_type_no;
                                            $scope.feeder_no = itemList[0].feeder_no;
                                            $scope.feeder_state = itemList[0].state;
                                            $scope.last_maintain_date = itemList[0].last_maintain_date;
                                        };
                                    });
                                // 80 報廢
                                } else if(itemList[0].state == '80'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.feeder_scrapped'));
                                // 70 送修
                                } else if(itemList[0].state == '70'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.feeder_repair'));
                                // 40 已上料 or 50 已上台車
                                } else if (itemList[0].state == '40' || itemList[0].state == '50'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.feeder_already_feeding'));
                                // 60 已上線
                                } else if (itemList[0].state == '60'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.feeder_already_online'));
                                // 不存在
                                } else {
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.no_feeder'));
                                };
                            } else {
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            };
                        });
                    // 物料流程
                    } else if($scope.phases_index == 1) {
                        material_info_get(barcode, function(itemList){
                            // 在庫狀態才能成功，其他狀態提示異常
                            if(itemList && itemList.length > 0){
                                if($scope.disable_control == '1'){
                                    if(itemList[0].forbidden_reel == '1'){//禁用物料
                                        $scope.$root.showAlert($filter('translate')('fordden_msg.forbid_reel'));
                                    }else{
                                        // MSD卡控卡這裡
                                        if((+itemList[0].remain_time) < 0 && itemList[0].remain_time !='-0.1'){
                                            $('#toast-panel').css("background-color","rgb(219, 75, 60)");
                                            $scope.$root.showAlert($filter('translate')('SMT03.msg.MSD_over_time'));
                                            return;
                                        }else if((+itemList[0].remain_time) <= 30 && (+itemList[0].remain_time) > 0){
                                            reel_process(itemList);
                                            $rootScope.showAlertWithoutfocus($filter('translate')('SMT03.msg.MSD_in_a_hurry'));
                                            return
                                        }else if((+itemList[0].remain_time) <= 60 && (+itemList[0].remain_time) > 30){
                                            reel_process(itemList);
                                            $rootScope.showAlertWithoutfocus($filter('translate')('SMT03.msg.MSD_go_to_over_time'));
                                            return
                                        }else{
                                            reel_process(itemList);
                                        }
                                    }
                                }else{
                                    // MSD卡控卡這裡
                                    if((+itemList[0].remain_time) < 0 && itemList[0].remain_time !='-0.1'){
                                        $('#toast-panel').css("background-color","rgb(219, 75, 60)");
                                        $scope.$root.showAlert($filter('translate')('SMT03.msg.MSD_over_time'));
                                        return;
                                    }else if((+itemList[0].remain_time) <= 30 && (+itemList[0].remain_time) > 0){
                                        reel_process(itemList);
                                        $rootScope.showAlertWithoutfocus($filter('translate')('SMT03.msg.MSD_in_a_hurry'));
                                        return
                                    }else if((+itemList[0].remain_time) <= 60 && (+itemList[0].remain_time) > 30){
                                        reel_process(itemList);
                                        $rootScope.showAlertWithoutfocus($filter('translate')('SMT03.msg.MSD_go_to_over_time'));
                                        return
                                    }else{
                                        reel_process(itemList);
                                    }
                                }
                            } else {
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            };
                        });
                    } else {
                        $scope.$root.showAlert($filter('translate')('SMT.common.msg.process_error'));
                    };
                };
                element_barcode_no.focus();
            };

            // 取得 Feeder資料
            function feeder_info_get(feeder_no, callback){
                $MMWService.pageNumberInfoGet({
                    page_mode : 'feeder_info_get',
                    query_condition_info:[
                        {column_name:'FeederNo', query_mode:'0', value: feeder_no, merge_condition_model:'AND'},
                    ]
                }, function(itemList){
                    if(callback)
                        callback(itemList);
                });
            };

            // Feeder檢查
            function feeder_check_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.feeder_mfu_check_process',
                    content : content,
                    success : function(data, response){
                        if(callback)
                            callback(data, response);
                    }
                });
            };

            // 取得 捲料資料
            function material_info_get(reel_no, callback){
                let content = {
                    reel_no:reel_no,
                }
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.reel_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // Feeder上料
            function feeder_reel_set_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.feeder_reel_set_process',
                    content : content,
                    success : function(data){
                        console.log(data);
                        if(callback)
                            callback(data);
                    }
                });
            };

            $scope.input_PDA_barcode = function(event){
                if (event.keyCode == 13) {
                    element_barcode_no.blur();
                    input_barcode_no_callback($scope.barcode)
                    $scope.barcode = '';
                } else {
                    $scope.barcode_plot_no = $scope.barcode_plot_no || '';
                }
            }

            //20200914 modify by Dustdusk for M#: 增加
            function makeBarcodeInput(el) {
                var _input = el.getElementsByClassName('barcode_input')[0];
                //var _mask = el.getElementsByClassName('barcode_mask')[0];
                el.focus = function () {
                    //e.preventDefault();
                    //_mask.click();
                    el.click();
                }
                el.addEventListener('click', function (e) {
                    e.preventDefault();
                    //_input.value = '';
                    if (document.activeElement !== _input)
                        _input.focus();
                });
                _input.addEventListener('focus', function (e) {
                    e.preventDefault();
                    setTimeout(function () {
                        _input.removeAttribute('readonly');
                    }, 200);
                });
                _input.addEventListener('blur', function (e) {
                    e.preventDefault();
                    _input.setAttribute('readonly', 'readonly');
                });
            }
        }
    ];
});
